import { Attribute } from "../etc/Attribute"
import { UIElementPool, UIPool } from "../etc/UIPool"
import { util } from "../etc/Util"
import { GameplayModuleC } from "../module/GameplayModule"
import { PlayerModuleC } from "../module/PlayerModule"
import MainUI_Generate from "../ui-generate/MainUI_generate"
import { Tween } from "../etc/Tween"
import { BagWindow } from "./BagWindow"

export class MainUI extends MainUI_Generate {
    private playerMD: PlayerModuleC
    private moneyPool: UIElementPool<Image>
    private powerPool: UIElementPool<Image>

    private moneyTextValue = 0
    private powerTextValue = 0

    onStart() {
        this.canUpdate = true

        this.btn_throw.onClicked.add(() => {
            ModuleService.getModule(GameplayModuleC).throwCharacter()
        })
        this.btn_bag.onClicked.add(() => {
            UIService.show(BagWindow)
        })

        this.moneyPool = new UIElementPool(() => {
            let img = Image.newObject()
            img.imageGuid = '136803'
            img.size = new Vector2(85, 85)
            this.rootCanvas.addChild(img)
            return img
        })
        this.powerPool = new UIElementPool(() => {
            let img = Image.newObject()
            img.imageGuid = '97787'
            img.size = new Vector2(85, 85)
            this.rootCanvas.addChild(img)
            return img
        })

        this.playerMD = ModuleService.getModule(PlayerModuleC)
    }

    onShow() {
        this.initEasingText()
    }

    onUpdate() {
        this.updateEasingText()
    }

    async showMoneyTransition(moneyCount: number, customStartPosition?:Vector2) {
        for (let i = 0; i < moneyCount; i++) {
            await TimeUtil.delaySecond(0.25)

            let money = this.moneyPool.get()
            money.position = customStartPosition || new Vector2(this.rootCanvas.size.x / 2 + util.randomNegative() * 600, this.rootCanvas.size.y / 2 + util.randomNegative() * 400)
            money.renderScale = util.getTemporaryVector2(1, 1)

            new Tween<{ t: number }>({ t: 0 }).to({ t: 1 }, 1500)
                .onUpdate(obj => {
                    money.position = Vector2.lerp(money.position, this.con_money.position, 0.1)
                    let scale = util.lerp(2, 1, obj.t)
                    money.renderScale = util.getTemporaryVector2(scale, scale)
                })
                .onComplete(() => {
                    this.moneyPool.giveBack(money)
                })
                .start()
        }
    }

    async showPowerTransition(powerCount: number, customStartPosition?:Vector2) {
        for (let i = 0; i < powerCount; i++) {
            await TimeUtil.delaySecond(0.25)

            let power = this.powerPool.get()
            power.position = customStartPosition || new Vector2(this.rootCanvas.size.x / 2 + util.randomNegative() * 600, this.rootCanvas.size.y / 2 + util.randomNegative() * 400)
            power.renderScale = util.getTemporaryVector2(1, 1)

            new Tween<{ t: number }>({ t: 0 }).to({ t: 1 }, 1500)
                .onUpdate(obj => {
                    power.position = Vector2.lerp(power.position, this.con_power.position, 0.1)
                    let scale = util.lerp(2, 1, obj.t)
                    power.renderScale = util.getTemporaryVector2(scale, scale)
                })
                .onComplete(() => {
                    this.powerPool.giveBack(power)
                })
                .start()
        }
    }

    private initEasingText(){
        let currentMoney = this.playerMD.getAttr(Attribute.EnumAttributeType.money)
        this.moneyTextValue = currentMoney
        this.txt_money.text = this.moneyTextValue.toString()
        
        let currentPower = this.playerMD.getAttr(Attribute.EnumAttributeType.power)
            + this.playerMD.getAttr(Attribute.EnumAttributeType.powerInTime)
        this.powerTextValue = currentPower
        this.txt_power.text = this.powerTextValue.toString()
    }

    private updateEasingText(){
        let currentMoney = this.playerMD.getAttr(Attribute.EnumAttributeType.money)
        if(this.moneyTextValue != currentMoney){
            this.moneyTextValue = Math.ceil(util.lerp(this.moneyTextValue, currentMoney, 0.05))
            this.txt_money.text = this.moneyTextValue.toString()
        }
        let currentPower = this.playerMD.getAttr(Attribute.EnumAttributeType.power) 
            + this.playerMD.getAttr(Attribute.EnumAttributeType.powerInTime)
        if(this.powerTextValue != currentPower){
            this.powerTextValue = Math.ceil(util.lerp(this.powerTextValue, currentPower, 0.05))
            this.txt_power.text = this.powerTextValue.toString()
        }
    }

}